package com.dyzx.hbb.modules.car.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.modules.car.entity.CarMark;
import com.dyzx.hbb.modules.car.service.CarMarkService;
import com.dyzx.hbb.modules.car.mapper.CarMarkMapper;
import com.dyzx.hbb.modules.car.service.CarModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【car_mark】的数据库操作Service实现
* @createDate 2025-08-08 14:08:16
*/
@Service
public class CarMarkServiceImpl extends ServiceImpl<CarMarkMapper, CarMark>
    implements CarMarkService{
    @Autowired
    private CarModelService carModelService;
    @Cacheable(value = "car", key = "'tree'")

    @Override
    public List<Map<String, Object>> findByTreeList() {
        List<Map<String, Object>> results=new ArrayList<>();
        MyLambdaQueryWrapper<CarMark> queryWrapper = new MyLambdaQueryWrapper<>();
        //FIND COUNT DESC MAX COUNT
        queryWrapper.orderByDesc(CarMark::getFindCount);
        queryWrapper.gt(CarMark::getFindCount,0);
        List< CarMark> list=this.list(queryWrapper);
        for (CarMark item : list) {
            Map<String, Object> map=new HashMap<>() ;
 
            map.put("id",item.getId());
            map.put("name",item.getName());
            map.put("logo",item.getLogo());
            map.put("first_letter",item.getFirstLetter());
            if(item.getFindCount()>0) {
                List<Map<String, Object>> children =carModelService.findByTreeList(item.getId());

                map.put("children", children);
            }else {
                map.put("children", List.of());
            }
            results.add(map);
        }
        return results;
    }
}




